package launcher;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Word {

	private String wordStr;
	private int score = -1;
	private HashMap<Integer, Integer> letterOccur;

	public Word(String wordStr) {
		this.wordStr = wordStr;
		this.score = getScore();
		this.letterOccur = getLetterOccur();
	}

	public String getStrMot() {
		return this.wordStr;
	}

	public void setStrMot(String strMot) {
		this.wordStr = strMot;
	}

	public int getScore() {

		if (score == -1) {
			if (letterOccur != null) {
				score = 0;
				Iterator<Entry<Integer, Integer>> it = letterOccur.entrySet()
						.iterator();

				while (it.hasNext()) {
					@SuppressWarnings("rawtypes")
					Map.Entry pairs = (Map.Entry) it.next();
					
					char a = (char)((int)pairs.getKey());

					
					Main.getLetterValues();
					score += (int)(pairs.getValue()) * Main.getLetterValues().get(a);
					it.remove();
				}
			}
		}
		return score;

	}

	@SuppressWarnings("boxing")
	public HashMap<Integer, Integer> getLetterOccur() {
		if (this.letterOccur == null) {
			this.letterOccur = new HashMap<>();
			int length = this.wordStr.length();

			for (int i = 0; i < length; i++) {
				int ascii = this.wordStr.charAt(i);
				if (this.letterOccur.get(ascii) == null) {
					this.letterOccur.put(ascii, 1);
				} else {
					this.letterOccur
							.put(ascii, this.letterOccur.get(ascii) + 1);
				}
			}
		}
		return this.letterOccur;
	}

	public void setLetterOccur(HashMap<Integer, Integer> letterOccur) {
		this.letterOccur = letterOccur;
	}

}
